package com.greenline.push.home.controllers.group;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.greenline.guser.annotation.GuserResource;
import com.greenline.push.common.json.BaseJson;
import com.greenline.push.common.json.GridJsonData;
import com.greenline.push.core.pushcenter.GroupUserManager;
import com.greenline.push.dal.pushcenter.dataobject.GroupUserDO;
import com.greenline.push.dal.query.GroupUserQuery;

/**
 * 
 * @Type GroupUserController
 * @Desc 群和用户关系
 * @author shuai.zuo
 * @date 2014年10月10日
 * @Version V1.0
 */
@Controller
@RequestMapping("/groupuser")
public class GroupUserController {
    @Resource
    private GroupUserManager groupUserManager;
    private final Log logger = LogFactory.getLog(GroupController.class);

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String queryRecord(ModelMap model) {
        return "groupuser/index";
    }

    @RequestMapping("/query.json")
    @ResponseBody
    public GridJsonData<GroupUserDO> listRecord(GroupUserQuery query,
            @RequestParam(value = "page", defaultValue = "1", required = false) String pageIndex,
            @RequestParam(value = "pageSize", defaultValue = "10", required = false) String pageSize) {
        query.setPageIndex(Integer.parseInt(pageIndex));
        query.setPageSize(Integer.parseInt(pageSize));
        GridJsonData<GroupUserDO> ret = new GridJsonData<GroupUserDO>();
        ret.setQuery(groupUserManager.listByQuery(query));
        return ret;
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String addInit(ModelMap model) {
        return "groupuser/add";
    }

    @RequestMapping(value = "/add.json", method = RequestMethod.POST)
    public @ResponseBody
    BaseJson add(ModelMap model, @ModelAttribute("groupuser") GroupUserDO groupuser) {
        BaseJson json = new BaseJson();
        try {
            if (groupUserManager.insert(groupuser) > 0) {
                json.setMessage("提交成功！");
            } else {
                json.setHasError(true);
                json.setMessage("提交出错");
            }
        } catch (Exception e) {
            json.setHasError(true);
            json.setMessage("提交出错");
            logger.error(json.getMessage(), e);
        }
        return json;
    }

    @RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
    public String updateInit(ModelMap model, @PathVariable Long id) {
        GroupUserDO object = groupUserManager.getById(id);
        model.put("groupuser", object);
        return "groupuser/update";
    }

    @RequestMapping(value = "/update.json", method = RequestMethod.POST)
    public @ResponseBody
    BaseJson update(ModelMap model, @ModelAttribute("groupuser") GroupUserDO groupuser) {
        BaseJson json = new BaseJson();
        try {
            if (groupUserManager.update(groupuser)) {
                json.setMessage("提交成功！");
            } else {
                json.setHasError(true);
                json.setMessage("提交出错");
            }
        } catch (Exception e) {
            json.setHasError(true);
            json.setMessage("提交出错");
            logger.error(json.getMessage(), e);
        }
        return json;
    }

    @GuserResource("删除群用户关系")
    @RequestMapping(value = "/delete.json", method = RequestMethod.POST)
    public @ResponseBody
    BaseJson delete(ModelMap model, @ModelAttribute("id") Long id) {
        BaseJson json = new BaseJson();
        try {
            if (groupUserManager.delete(id)) {
                json.setMessage("删除成功！");
            } else {
                json.setHasError(true);
                json.setMessage("删除出错");
            }
        } catch (Exception e) {
            json.setHasError(true);
            json.setMessage("删除出错");
            logger.error(json.getMessage(), e);
        }
        return json;
    }
}
